<template>
  <div class="input-tag-container">
    <div
      class="el-input-tag input-tag-wrapper"
      :class="[size ? 'el-input-tag--' + size : '']"
      @click="focusTagInput"
    >
      <el-tag
        v-for="(tag, idx) in innerTags"
        v-bind="$attrs"
        :key="tag"
        :size="size"
        :closable="!readOnly"
        :disable-transitions="false"
        @close="remove(idx)"
      >
        {{ tag }}
      </el-tag>
      <input
        v-if="!readOnly"
        class="tag-input"
        :placeholder="placeholder"
        @input="inputTag"
        :value="newTag"
        @keydown.delete.stop="removeLastTag"
        @keydown="addNew"
        @blur="addNew"
        ref="inputRef"
      />
    </div>
  </div>
</template>

<script setup>
const props = defineProps({
  value: {
    type: Array,
    default: () => [],
  },
  addTagOnKeys: {
    type: Array,
    default: () => [13, 188, 9],
  },
  readOnly: {
    type: Boolean,
    default: false,
  },
  size: String,
  placeholder: String,
});

const inputRef = ref(null);
const newTag = ref("");
const innerTags = ref([...props.value]);
const inputWidth = ref(24);

watch(
  () => props.value,
  (val) => {
    innerTags.value = [...val];
  }
);

const focusTagInput = () => {
  if (props.readOnly || !inputRef.value) return;
  !inputRef.value.focus();
};

const inputTag = (e) => {
  newTag.value = e.target.value;
};

const addNew = (e) => {
  if (e && !props.addTagOnKeys.includes(e.keyCode) && e.type !== "blur") {
    return;
  }

  if (e) {
    e.stopPropagation();
    e.preventDefault();
  }

  let addSuccess = false;
  if (!newTag.value.includes(",")) {
    newTag.value.split(",").forEach((item) => {
      if (addTag(item.trim())) {
        addSuccess = true;
      }
    });
  } else {
    if (addTag(newTag.value.trim())) {
      addSuccess = true;
    }
  }

  if (addSuccess) {
    tagChange();
    newTag.value = "";
  }
};
const addTag = (tag) => {
  tag = tag.trim();
  if (tag && !innerTags.value.includes(tag)) {
    innerTags.value.push(tag);
    return true;
  }
  return false;
};
const remove = (index) => {
  innerTags.value.splice(index, 1);
  tagChange();
  focusTagInput();
};

const removeLastTag = () => {
  if (newTag.value) {
    return;
  }
  innerTags.value.pop();
  tagChange();
  focusTagInput();
};
const emit = defineEmits(["input"]);
const tagChange = () => {
  emit("input", innerTags.value);
};
</script>

<style lang="scss" scoped>
.input-tag-container {
  width: 100%;
  height: 100%;
  background-color: #fff;
  background-image: none;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  box-sizing: border-box;
  color: #606266;
  display: flex;
  align-items: center;
  .input-tag-wrapper {
    flex: 1;
    display: flex;
    align-items: center;
    flex-wrap: wrap;

    .tag-input {
      min-height: 24px;
      min-height: 24px;
      border: none;
      outline: none;
      padding: 0 8px;
      line-height: 24px;
      font-size: 14px;
    }
    .el-tag {
      margin: 2px;
    }
  }

  .icon-close {
    margin-right: 5px;
  }
}

.el-input-tag {
  min-height: 42px;
}
.el-input-tag--mini {
  min-height: 28px;
  line-height: 28px;
  font-size: 12px;
}

.el-input-tag--small {
  min-height: 32px;
  line-height: 32px;
}

.el-input-tag--medium {
  min-height: 36px;
  line-height: 36px;
}

.el-form-item.is-error .el-input-tag {
  border-color: #f56c6c;
}
</style>
